package com.bd.rj.frontstage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bd.rj.common.entity.Collections;
import com.bd.rj.frontstage.service.impl.CollectionsServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class CollectionController {

    @Resource
    CollectionsServiceImpl collectionsService;

    /**
     * @param uid
     * @param pid
     * @return
     * @desc 收藏和取消收藏
     * @author yxh
     */
    @PostMapping("/collections")
    public Map<String, Object> insertCollections(Long uid, Long pid) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uid", uid);
        wrapper.eq("pid", pid);
        List list = collectionsService.list(wrapper);
        Collections collections = new Collections();
        if (list.size() == 0) {
            collections.setUid(uid);
            collections.setPid(pid);
            collectionsService.save(collections);
            map.put("msg", "收藏成功");
            map.put("code", 200);
            map.put("data", collections);
        } else {
            boolean isDeleted = collectionsService.remove(wrapper);
            if (isDeleted) {
                map.put("msg", "取消收藏");
                map.put("code", 400);
            }
        }
        return map;
    }

    /**
     * @desc 取消收藏
     * @author yxh
     * @timedate
     */
    @PostMapping("/uncollects")
    public Map<String, Object> removeCollection(Long uid, Long pid) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Collections> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("pid", pid);
        boolean isDeleted = collectionsService.remove(wrapper);
        if (isDeleted) {
            map.put("msg", "取消收藏成功");
            map.put("code", 200);
        } else {
            map.put("msg", "取消收藏失败");
            map.put("code", 500);
        }
        return map;
    }


    /**
     * @param uid
     * @return
     * @desc 展示收藏的岗位信息
     * @author yxh
     */
    @GetMapping("/getcollections")
    public Map<String, Object> getAllCollectionsByUid(Long uid) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", collectionsService.getAllCollections(uid));
        return map;
    }

    /**
     * @desc 判断是否收藏
     * @author yxh
     * @timedate
     */
    @GetMapping("/shifoushoucang")
    public Map<String, Object> shifoushoucang(Long uid, Long pid) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("uid", uid);
        wrapper.eq("pid", pid);
        List list = collectionsService.list(wrapper);
        if (list.size() == 0) {
            map.put("msg", "未收藏");
            map.put("code", 400);
        } else {
            map.put("msg", "已收藏");
        }
        return map;
    }
    /**
     *
     * 批量删除
     * @author WBW
     * @param idList
     * @return
     */
    @PostMapping("/delete")
    public Map<String, Object> delete(@RequestBody List<Long> idList) {
        Map<String, Object> map = new HashMap<>();
        collectionsService.batchDeleteByIds(idList);
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", "删除成功");
        return map;
    }


}
